package com.zmzncs.lmtc.module.customer.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zmzncs.lmtc.common.pojo.LoginUser;
import com.zmzncs.lmtc.common.pojo.enums.optionalvalue.promotion.MonthRentTypeEnum;
import com.zmzncs.lmtc.common.util.DateTimeUtil;
import com.zmzncs.lmtc.common.util.EnumUtil;
import com.zmzncs.lmtc.common.util.JwtUtil;
import com.zmzncs.lmtc.module.customer.dao.CardDao;
import com.zmzncs.lmtc.module.customer.dao.CardRecordDao;
import com.zmzncs.lmtc.module.customer.dao.CardRecordParkDao;
import com.zmzncs.lmtc.module.customer.pojo.card.CardDTO;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecord;
import com.zmzncs.lmtc.module.customer.pojo.card_record.CardRecordDTO;
import com.zmzncs.lmtc.module.customer.pojo.card_record_park.CardRecordParkDTO;
import com.zmzncs.lmtc.module.customer.service.CardRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 客户-会员卡购买记录 服务实现类
 * </p>
 *
 * @author 任建波
 * @since 2020-09-02
 */
@Service
public class CardRecordServiceImpl extends ServiceImpl<CardRecordDao, CardRecord> implements CardRecordService {


    @Autowired
    CardDao cardDao;
    @Autowired
    CardRecordParkDao cardRecordParkDao;
    @Autowired
    CardRecordDao cardRecordDao;
    @Autowired
    HttpServletRequest request;

    /**
     * 查询月租卡购买记录
     */
    @Override
    public List<CardRecordDTO> getMonthCardRecord() {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        /**查询月租卡购买记录*/
        List<CardRecordDTO> monthCardRecordList = cardRecordDao.getMonthCardRecord(loginUser.getCustomerId());
        if(monthCardRecordList != null && monthCardRecordList.size() > 0){
            for(CardRecordDTO cardRecordDTO: monthCardRecordList){
                long distanceDays = DateTimeUtil.getDistanceDays(cardRecordDTO.getBeginTime(), cardRecordDTO.getExpireTime());
                cardRecordDTO.setMonthCardDays((int) distanceDays);
                /**查询月卡购买记录关联停车场信息*/
                CardRecordParkDTO cardRecordParkDTO = cardRecordParkDao.getCardRecordParkByRecordId(cardRecordDTO.getId());
                if (cardRecordParkDTO != null){
                    cardRecordDTO.setParkName(cardRecordParkDTO.getParkName());
                }
                CardDTO cardDTO = cardDao.getCardAndCarByCardId(cardRecordDTO.getCardId());

//                cardRecordDTO.getMonthRentType();
                cardRecordDTO.setCarNumber(cardDTO.getCarNumber());
            }
        }
        return monthCardRecordList;
    }

    /**
     * 查询时长卡购买记录
     */
    @Override
    public List<CardRecordDTO> getDurationCardRecord() {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        List<CardRecordDTO> durationCardRecordList = cardRecordDao.getDurationCardRecord(loginUser.getCustomerId());

        return durationCardRecordList;
    }

    @Override
    public List<CardRecordDTO> getRechargeCardRecord() {
        LoginUser loginUser = JwtUtil.getLoginUser(request);
        List<CardRecordDTO> rechargeCardRecordList = cardRecordDao.getRechargeCardRecord(loginUser.getCustomerId());

        return rechargeCardRecordList;
    }

}
